package com.ct.controller.shipping;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.shipping.model.po.ChuanYuanKuSavePO;
import com.ct.application.shipping.model.po.ChuanYuanKuSearchPO;
import com.ct.application.shipping.model.vo.ChuanYuanKuVO;
import com.ct.application.shipping.service.ChuanYuanKuDaoRuService;
import com.ct.application.shipping.service.ChuanYuanKuService;
import com.ct.commons.BaseController;
import com.ct.commons.annotation.*;
import com.ct.commons.aspect.limiter.AccessIpLimiter;
import com.ct.utils.MyEasyExcel;
import com.ct.utils.exception.ApiException;
import com.ct.utils.jwt.UserInfo;
import com.ct.utils.threadlocal.UserThreadLocal;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

/**
 * 船员资料配置
 * @author Auto Generator
 * @since 2023-08-16
 */
@RestController
@PermissionApi(title = "船员资料",path="/api/v1/chuanyuanku",code = "V1000",sorted = 9)
public class ChuanYuanKuController extends BaseController {

    @Resource
    private ChuanYuanKuService chuanYuanKuService;
    @Resource
    private ChuanYuanKuDaoRuService chuanYuanKuDaoRuService;


    /**
     * 查询船员资料列表
     * @return
     */
    @PostMapping("/chuanyuanku/listbiao")
    @PermissionApi(title = "查询船员资料列表",path="/api/v1/chuanyuanku/listlistbiao",code = "V1007",sorted = 7)
    public List<ChuanYuanKuVO> findChuanYuanKuBiao() {
        return chuanYuanKuService.findChuanYuanKuBiao();
    }

    /**
     * 查询船员资料列表
     * @param chuanYuanKuSearchPO
     * @return
     */
    @PostMapping("/chuanyuanku/list")
    @PermissionApi(title = "查询船员资料列表",path="/api/v1/chuanyuanku/list",code = "V1001",sorted = 1)
    public Page<ChuanYuanKuVO> findChuanYuanKus(@Validated @RequestBody ChuanYuanKuSearchPO chuanYuanKuSearchPO) {
        return chuanYuanKuService.findChuanYuanKu(chuanYuanKuSearchPO);
    }

    /**
     * 查询船员资料列表-用于分组
     * @param chuanYuanKuSearchPO
     * @return
     */
    @PostMapping("/chuanyuanku/grouplist")
    @PermissionApi(title = "查询船员资料列表-用于分组",path="/api/v1/chuanyuanku/grouplist",code = "V1009",sorted = 9)
    public Page<ChuanYuanKuVO> findChuanYuanKuGroup(@Validated @RequestBody ChuanYuanKuSearchPO chuanYuanKuSearchPO) {
        return chuanYuanKuService.findChuanYuanKuGroup(chuanYuanKuSearchPO);
    }

    /**
     * 保存和更新
     *
     * @param savePO
     * @return
     */
    @PostMapping("/chuanyuanku/saveupdate")
    @ApiLog("船员资料保存和更新")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "船员资料保存和更新",path="/api/v1/chuanyuanku/saveupdate",code = "V1003",sorted = 3)
    public ChuanYuanKuVO saveUpdateChuanYuanKu(@Validated @RequestBody ChuanYuanKuSavePO savePO) {
        savePO.setLaiYuan("2");
        if (Objects.nonNull(savePO.getId())){
            return chuanYuanKuService.updateChuanYuanKu(savePO);
        }else {
            return chuanYuanKuService.saveChuanYuanKu(savePO);
        }
    }


    /**
     * 根据id查询船员资料
     *
     * @param id
     * @return
     */
    @PostMapping("/chuanyuanku/get/{id}")
    @PermissionApi(title = "根据id查询船员资料",path="/api/v1/chuanyuanku/get/{id}",code = "V1004",sorted = 4)
    public ChuanYuanKuVO getChuanYuanKuById(@PathVariable("id") Long id) {
        return chuanYuanKuService.getChuanYuanKu(id);
    }

    /**
     * 根据id删除船员资料
     *
     * @param id
     * @return
     */
    @PostMapping("/chuanyuanku/del/{id}")
    @ApiLog("根据id删除船员资料")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "根据id删除船员资料",path="/api/v1/chuanyuanku/del/{id}",code = "V1005",sorted = 5)
    public Boolean delChuanYuanKuById(@PathVariable("id") Long id) {
        return chuanYuanKuService.delChuanYuanKu(id);
    }

    /**
     * 修改状态
     *
     * @return
     */
    @PostMapping("/chuanyuanku/update/status")
    @ApiLog("船员资料修改状态")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "船员资料修改状态",path="/api/v1/chuanyuanku/update/status",code = "V1006",sorted = 6)
    public Boolean updateStatus(@Validated @RequestBody StatusRequest statusRequest) {
        return chuanYuanKuService.updateStatus(statusRequest);
    }




    @PostMapping("/chuanyuanku/chuanYuanDaoRu")
    @AccessIpLimiter
    @PermissionApi(title = "船员导入",path="/api/v1/chuanyuanku/chuanYuanDaoRu",code = "V1010",sorted = 10)
    public boolean chuanYuanDaoRu(MultipartFile file) throws IOException {
        if(!MyEasyExcel.checkFileType(file,true)){
            throw new RuntimeException("非法的文件格式,只允许导入xls/xlsx/cvs");
        }
        String fileType = MyEasyExcel.getFileType(file);
        chuanYuanKuDaoRuService.chuanYuanDaoRu(file.getInputStream(),fileType);
        return  true;
    }
}
